package com.beyond.pm.component.chart

import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.viewinterop.AndroidView
import com.beyond.pm.R
import com.beyond.theme.orange500
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.utils.ColorTemplate

/**
 * @author chen
 * @time 2023/6/12 14:40
 * @name ProjectManage
 * @class describe
 */
@Composable
fun PieChart(
    modifier: Modifier = Modifier,
    entries: List<PieEntry>,
    dataSetLabel: String,
    animateX: Int = 500,
    animateY: Int = 500
) {
    var chart by remember { mutableStateOf<PieChart?>(null) }
    val tColor = MaterialTheme.colorScheme.onBackground.toArgb()
    Box(modifier = modifier.fillMaxSize()) {
        Surface(color = MaterialTheme.colorScheme.background) {
            AndroidView(
                factory = { context ->
                    PieChart(context).apply {
                        layoutParams = ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                        )
                        data = PieData(PieDataSet(entries, ""))
                        description = Description().apply {
                            text = ""
                        }
                        legend.apply {
                            textColor = tColor
                            horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
                            verticalAlignment = Legend.LegendVerticalAlignment.TOP
                            orientation = Legend.LegendOrientation.VERTICAL
                            xOffset = 10f
                        }
                        centerText = dataSetLabel
                        setDrawCenterText(true) // 是否显示中间的文字
                        holeRadius = 45f // 设置饼图的中心空白区域半径为20
                        transparentCircleRadius = 55f // 设置饼图的透明圆环半径为30
                        isDrawHoleEnabled = true // 用于控制是否绘制中心空白区域
                        setNoDataText(context.getString(R.string.no_data))
                        setTouchEnabled(true)
                        setDrawEntryLabels(true) // 是否显示实体对象的标题
                        animateX(animateX)
                        animateY(animateY)
                    }
                },
                update = { view ->
                    chart = view
                    updateChart(chart, entries, dataSetLabel)
                }
            )
        }
    }
}

private fun updateChart(
    chart: PieChart?,
    entries: List<PieEntry>,
    dataSetLabel: String
) {
    chart?.let {
        val dataSet = PieDataSet(entries, dataSetLabel)
        dataSet.colors = ColorTemplate.MATERIAL_COLORS.toList() // 使用随机颜色
        dataSet.label = ""
        dataSet.valueTextSize = 10f
        dataSet.valueTextColor = orange500.toArgb()
        val pieData = PieData(dataSet)
        it.data = pieData
        it.invalidate()
    }
}
